package com.zxx.pattern.factoryPattern.factoryAbstract;

import com.zxx.pattern.factoryPattern.factorySample.BMW;

import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @author zxx on 2020/12/28
 */
public class Test {

    private static BMW bmw;
    private static AC ac;

    private static final String[] classes = {
            "com.zxx.pattern.factoryAbstract.Factory523",
            "com.zxx.pattern.factoryAbstract.Factory320"};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择要生产的产品的型号：");
        System.out.println("1、523型号");
        System.out.println("2、320型号");
        int type = scanner.nextInt();

        try {
            Class<?> factory = Class.forName(classes[type - 1]);
            Method createBMW = factory.getMethod("createBMW");
            Method createAC = factory.getMethod("createAC");

            bmw = (BMW) createBMW.invoke(factory.newInstance());
            ac = (AC) createAC.invoke(factory.newInstance());

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (bmw != null && ac != null) {
            bmw.introduce();
            ac.introduce();
        } else {
            System.out.println("没有这个型号的产品！");
        }
        scanner.close();

    }


}
